<?php

namespace Biz\model\activity;


class BizProjectPRelationIdsModel extends BizProjectBaseModel
{
    const _TABLE_NAME = 'biz_project_relation_ids';

    use BizProjectTrait;

    const TYPE_PREPARE = 1; //商业售前项目
    const TYPE_PROJECT = 2; //商业项目

    public static function findPrePid($rid, $type = 0)
    {
        return self::findPid(self::TYPE_PREPARE, $rid, $type);
    }

    public static function findProjectPid($rid, $type = 0)
    {
        return self::findPid(self::TYPE_PROJECT, $rid, $type);
    }

    public static function findPid($type, $rid, $pType)
    {
        $list = self::dataSelect(['type' => $type, 'rid' => $rid, 'p_type' => $pType] + self::statusCondition(), 'pid');
        return empty($list) ? [] : $list;
    }
    public static function getPreRelationIds($id, $type = 0): array
    {
        return $id ? self::getRelationsIds($id, self::TYPE_PREPARE, $type) : [];
    }
    public static function getProjectRelationIds($id, $type = 0): array
    {
        return self::getRelationsIds($id, self::TYPE_PROJECT, $type);
    }
    public static function getRelationsIds($pid, $type = 0, $p_type = 0): array
    {
        $list = self::dataSelect(['type' => $type, 'pid' => $pid, 'p_type' => $p_type] + self::statusCondition(), ['pid','rid']);
        $multi = is_array($pid);
        $response = [];
        if ($list) {
            foreach ($list as $v) {
                if ($multi) {
                    $response[$v['pid']][] = $v['rid'];
                } else {
                    $response[] = $v['rid'];
                }
            }
        }
        return $response;
    }

    public static function saveProjectRelationIds($id, $rid, $type = 0)
    {
        self::saveRelationIds($id, $rid, self::TYPE_PROJECT, $type);
    }

    public static function savePreRelationIds($id, $rid, $type = 0)
    {
        self::saveRelationIds($id, $rid, self::TYPE_PREPARE, $type);
    }
    public static function saveRelationIds($id, $rid, $pType, $type = 0)
    {
        if (is_scalar($rid)) $rid = explode(',', $rid);
        //查询已经存在的 id
        $exists = self::dataSelect(['type' => $pType, 'pid' => $id, 'p_type' => $type]);
        $existsMap = [];
        if ($exists) {
            foreach ($exists as $item) {
                $existsMap[$item['rid']] = $item;
            }
        }
        $rows = [];
        if ($rid) {
            foreach ($rid as $v) {
                $find = $existsMap[$v] ?? [];
                if ($find) {
                    $existId = $find['id'];
                    if (!self::isNormal($find['status'])) {
                        //恢复记录状态
                        $rows[] = ['id' => $existId, 'status' => self::normalStatus()];
                    }
                    unset($existsMap[$v]);
                    continue;
                }
                //新增记录
                $rows[] = ['type' => $pType, 'pid' => $id, 'p_type' => $type, 'rid' => $v];
            }
        }
        //删除没有处理的
        foreach ($existsMap as $item) {
            $rows[] = ['id' => $item['id'], 'status' => self::delStatus()];
        }
        //批量执行语句
        foreach ($rows as $row) {
            self::addData($row);
        }
    }


}
